//微信原生方法封装
const http = require('../utils/http')

const handle = {
    getOpenid: function () {
        return new Promise((resolve, reject) => {
            let openid = wx.getStorageSync("openid");
            //相当于判断openid=""、openid=null、openid = undefined、openid=0
            if (!openid) {
                wx.login({
                    success: res => {
                        // 发送 res.code 到后台换取 openId, sessionKey, unionId
                        http._get('/mini/login?code=' + res.code).then(res => {
                            wx.setStorageSync("openid", res.result.openid);
                            wx.setStorageSync("sessionKey", res.result.sessionKey);
                            resolve(res.result.openid)
                        })
                    }
                })
            } else {
                resolve(openid)
            }
        })
    },
}

module.exports = handle